package com.fastmms;

import com.fastmms.utils.ByteUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestTemp {


    public static void main(String[] args) throws Exception {
        String path = System.getProperty("user.dir") + "/testFile/temp/";
        File dir = new File(path);

        List<File> indexList = new ArrayList<>();
        Map<String, File> tempMap = new HashMap<>();
        File[] fileList = dir.listFiles();

        String name;
        for (File file : fileList) {
            name = file.getName();
            if (name.endsWith(".temp")) {
                tempMap.put(name.replace(".temp", ""), file);
            } else if (name.endsWith(".index")) {
                //indexMap.put( name.replace(".index","") , file );
                indexList.add(file);
            }
        }

        FileOutputStream txtOut = new FileOutputStream(path + "temp.txt");

        int sumSize = 0;
        for (File indexFile : indexList) {
            name = indexFile.getName().replace(".index", "");
            File tempFile = tempMap.get(name);

            RandomAccessFile accessIndexFile = new RandomAccessFile(indexFile, "r");
            RandomAccessFile accessTempFile = new RandomAccessFile(tempFile, "r");

            long lastIndex = 0;
            long length = accessIndexFile.length();
            long msgSize = 0;
            while (accessIndexFile.getFilePointer() + 1 < length) {

                long i = accessIndexFile.readLong();
                int blockSize = (int) (i - lastIndex);
                byte[] blockSizeData = new byte[blockSize];

                accessTempFile.seek(lastIndex);
                accessTempFile.read(blockSizeData, 0, blockSize);

                int point = 0;
                while (true) {
                    if (point + 3 >= blockSize) {
                        break;
                    }
                    int keySize = ByteUtil.toInt(blockSizeData, point);
                    point += 4;
                    byte[] keyData = new byte[keySize];
                    System.arraycopy(keyData,0, blockSizeData, point , keySize);
                    point += keySize;

                    int valueSize = ByteUtil.toInt(blockSizeData, point);
                    point += 4;
                    byte[] valueData = new byte[valueSize];
                    System.arraycopy(valueData,0,blockSizeData, point, valueSize);
                    point += valueSize;

                    String msg = new String(keyData) + " : " + new String(valueData) + "\n";
                    System.out.println(msg);
                    /*txtOut.write( msg.getBytes(StandardCharsets.UTF_8) );
                    txtOut.flush();*/
                }
                msgSize++;
                sumSize += msgSize;
            }
            System.out.println("文件 " + tempFile.getName() + "读取条数：" + msgSize);
        }
        txtOut.close();
        System.out.println("读取结束，共读取 " + sumSize + "条");
       /* for (File tempFile : fileList){
            if(!tempFile.getName().endsWith(".temp")){
                continue;
            }
            long length = tempFile.length();
            long size = 0;
            RandomAccessFile accessFile = new RandomAccessFile(tempFile,"r");
            while (accessFile.getFilePointer() + 1 < length){
                int ks = accessFile.readInt();
                byte[] kb = new byte[ks];
                accessFile.read(kb,0,ks);

                int vs = accessFile.readInt();
                byte[] vb = new byte[vs];
                accessFile.read(vb,0,vs);
                size++;
            }
            sum += size;
            System.out.println("读取文件：" + tempFile.getName() + " / 读取消息数 ：" + size);
            accessFile.close();
        }
        System.out.println("共读取消息数：" + sum);*/

    }
}
